<template>
  <div class="lt-wrapper">
    <n-form>
      <n-form-item label="用户名">
        <n-input v-model:value="loginForm.username"></n-input>
      </n-form-item>
      <n-form-item label="密码">
        <n-input v-model:value="loginForm.password"></n-input>
      </n-form-item>
    </n-form>

    <n-button @click="submitLogin"> 后台 </n-button>
    <n-button @click="forTest"> 测试 </n-button>
    <n-button @click="submitSignUp"> 前台 </n-button>
  </div>
</template>

<script>
import uApi from '/src/api/base/u_api.js'
import cookieApi from '/src/api/cookie_api.js'
import globalStore from '/src/components/pinia/store.js'
import base64Tool from '/src/utils/encrypt/base64-tool.js'

const store = globalStore()
const cookieName = import.meta.env.VITE_COOKIE_AUTH

export default {
  setup() {
    
  },
  inject: [ 'Message' ],
  data () {
    return {
      loginForm: {
        username: 'wan',
        password: '123',
        rememberMe: false,
      }
    }
  },

  methods: {
    submitLogin() {
      let data = {
        username: base64Tool.encodeReqData(this.loginForm.username),
        password: base64Tool.encodeReqData(this.loginForm.password)
      }

      // request.post("/base/u/login", data)
      // uApi.login(data)
      uApi.loginEnc(data)
      .then(res => {
        // console.log(res.data)
        let userInfo = res.data
        // this.Store.setToken(userInfo.token)
        // this.Store.setUserinfo(userInfo)
        // this.Message.success( "登录成功", { closable: true, duration: 3000 } )
        store.setToken(userInfo.token)
        store.setUserinfo(userInfo)
        this.Message.success( "登录成功", { closable: true, duration: 3000 } )

        // cookieApi.removeCookie(cookieName)
        // if(cookieName) {
        //   cookieApi.setCookie(cookieName, res.data, 7*24*60*60)
        // }
        let from = this.$route.query.from
        from = from ? from : '/manage/index'
        if(from) {
          if( from.startsWith("http")) {
            window.location.href = from
          } else {
            this.$router.push(from)
          }
        }
      }, err => {
        this.Message.error( err.message, { closable: true, duration: 3000 } )
      })
    },
    forTest() {
      console.log("store-token", store.token)
      if(cookieName) {
        console.log("cookie-token", cookieApi.getCookie(cookieName))
      }
      
    },
    submitSignUp() {
      let data = {
        username: base64Tool.encodeReqData(this.loginForm.username),
        password: base64Tool.encodeReqData(this.loginForm.password)
      }

      // request.post("/base/u/login", data)
      // uApi.login(data)
      uApi.signupEnc(data)
      .then(res => {
        // console.log(res.data)
        let userInfo = res.data
        // this.Message.success( "登录成功", { closable: true, duration: 3000 } )
        store.setToken(userInfo.token)
        store.setUserinfo(userInfo)
        this.Message.success( "登录成功", { closable: true, duration: 3000 } )

        let from = this.$route.query.from
        from = from ? from : '/home'
        if(from) {
          this.$router.push(from)
        }
      }, err => {
        this.Message.error( err.message, { closable: true, duration: 3000 } )
      })
    },
  }
}
</script>

<style scoped>
.lt-wrapper {
  margin: 3rem auto 4rem auto;
  width: 60rem;
  text-align: left;
}
</style>
